package com.kwdb.sdk.query;

import com.kwdb.sdk.KWDBClient;
import com.kwdb.sdk.exception.KWDBException;

/**
 * 查询构建器，提供链式API构建SQL查询
 */
public class QueryBuilder {
    
    private final KWDBClient client;
    private final StringBuilder sql;
    
    public QueryBuilder(KWDBClient client) {
        this.client = client;
        this.sql = new StringBuilder();
    }
    
    /**
     * 选择字段
     * @param fields 字段列表
     * @return QueryBuilder
     */
    public QueryBuilder select(String... fields) {
        sql.append("SELECT ");
        if (fields.length == 0) {
            sql.append("*");
        } else {
            sql.append(String.join(", ", fields));
        }
        return this;
    }
    
    /**
     * 从指定表查询
     * @param table 表名
     * @return QueryBuilder
     */
    public QueryBuilder from(String table) {
        sql.append(" FROM ").append(table);
        return this;
    }
    
    /**
     * 添加WHERE条件
     * @param condition 条件
     * @return QueryBuilder
     */
    public QueryBuilder where(String condition) {
        sql.append(" WHERE ").append(condition);
        return this;
    }
    
    /**
     * 添加AND条件
     * @param condition 条件
     * @return QueryBuilder
     */
    public QueryBuilder and(String condition) {
        sql.append(" AND ").append(condition);
        return this;
    }
    
    /**
     * 添加OR条件
     * @param condition 条件
     * @return QueryBuilder
     */
    public QueryBuilder or(String condition) {
        sql.append(" OR ").append(condition);
        return this;
    }
    
    /**
     * 添加GROUP BY子句
     * @param fields 分组字段
     * @return QueryBuilder
     */
    public QueryBuilder groupBy(String... fields) {
        sql.append(" GROUP BY ").append(String.join(", ", fields));
        return this;
    }
    
    /**
     * 添加ORDER BY子句
     * @param field 排序字段
     * @param direction 排序方向
     * @return QueryBuilder
     */
    public QueryBuilder orderBy(String field, String direction) {
        sql.append(" ORDER BY ").append(field).append(" ").append(direction);
        return this;
    }
    
    /**
     * 添加LIMIT子句
     * @param limit 限制数量
     * @return QueryBuilder
     */
    public QueryBuilder limit(int limit) {
        sql.append(" LIMIT ").append(limit);
        return this;
    }
    
    /**
     * 添加OFFSET子句
     * @param offset 偏移量
     * @return QueryBuilder
     */
    public QueryBuilder offset(int offset) {
        sql.append(" OFFSET ").append(offset);
        return this;
    }
    
    /**
     * 执行查询
     * @return 查询结果
     * @throws KWDBException 查询异常
     */
    public QueryResult execute() throws KWDBException {
        return client.query(sql.toString());
    }
    
    /**
     * 异步执行查询
     * @return CompletableFuture<QueryResult>
     */
    public java.util.concurrent.CompletableFuture<QueryResult> executeAsync() {
        return client.queryAsync(sql.toString());
    }
    
    /**
     * 流式执行查询
     * @return CompletableFuture<QueryResult>
     */
    public java.util.concurrent.CompletableFuture<QueryResult> executeStream() {
        return client.queryStreamAsync(sql.toString());
    }
    
    /**
     * 获取SQL字符串
     * @return SQL字符串
     */
    public String toSql() {
        return sql.toString();
    }
    
    @Override
    public String toString() {
        return toSql();
    }
}
